package com.yanfan.energy.service.impl;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.energy.domain.EquipmentConsumptionDto;
import com.yanfan.energy.domain.OverallEnergy;
import com.yanfan.energy.domain.enums.EnergyType;
import com.yanfan.energy.domain.statist.DateStatistics;
import com.yanfan.energy.mapper.OverallEnergyDao;
import com.yanfan.energy.service.EnergyPriceCache;
import com.yanfan.energy.service.OverallEnergyService;
import com.yanfan.energy.service.TotalEnergyService;
import com.yanfan.energy.util.DateUtils;
import com.yanfan.energy.util.DoubleOperation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 能源总表(OverallEnergy)表服务实现类
 *
 * @author makejava
 * @since 2022-09-26 13:57:15
 */
@Service("overallEnergyService")
@Transactional(rollbackFor = Exception.class)
public class OverallEnergyServiceImpl implements OverallEnergyService {

    @Autowired
    TotalEnergyService totalEnergyService;
    @Autowired
    EnergyPriceCache energyPriceCache;
    @Resource
    private OverallEnergyDao overallEnergyDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OverallEnergy queryById(Integer id) {
        return this.overallEnergyDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param overallEnergy 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<OverallEnergy> queryByPage(OverallEnergy overallEnergy, PageRequest pageRequest) {
        long total = this.overallEnergyDao.count(overallEnergy);
        return new PageImpl<>(this.overallEnergyDao.queryAllByLimit(overallEnergy, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param overallEnergy 实例对象
     * @return 实例对象
     */
    @Override
    public OverallEnergy insert(OverallEnergy overallEnergy) {
        this.overallEnergyDao.insert(overallEnergy);
        return overallEnergy;
    }

    /**
     * 修改数据
     *
     * @param overallEnergy 实例对象
     * @return 实例对象
     */
    @Override
    public OverallEnergy update(OverallEnergy overallEnergy) {
        this.overallEnergyDao.update(overallEnergy);
        return this.queryById(overallEnergy.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.overallEnergyDao.deleteById(id) > 0;
    }

    @Override
    public void deleteByCreatdateandEquipment(String time, String id) {
        this.overallEnergyDao.deleteByCreatdateandEquipment(time, id);
    }

    @Override
    public OverallEnergy selectBySerialNumber(String id) {
        return overallEnergyDao.selectBySerialNumber(id);
    }

    @Override
    public AjaxResult selectequipmentEnergy(Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, Integer floor) {
        List<EquipmentConsumptionDto> overallEnergyDTOS = new ArrayList<>();

        if (SecurityUtils.getUserId() == 256) {
            overallEnergyDTOS = overallEnergyDao.selectequipmentEnergy(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor, 20);
        } else {
            overallEnergyDTOS = overallEnergyDao.selectequipmentEnergy(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor, null);
        }
        if (overallEnergyDTOS != null && overallEnergyDTOS.size() > 0) {
            for (EquipmentConsumptionDto overallEnergyDTO : overallEnergyDTOS) {
                //用电统计
                overallEnergyDTO.setPowerConsumption(DoubleOperation.add(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPeaksType(), overallEnergyDTO.getParityType(), overallEnergyDTO.getLowebbType()));
                //用水统计
                overallEnergyDTO.setWaterConsumption(DoubleOperation.add(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getTapType(), overallEnergyDTO.getSofteningType()));
                //用气统计
                overallEnergyDTO.setGasConsumption(DoubleOperation.add(overallEnergyDTO.getCompressType(), overallEnergyDTO.getNaturalType(), overallEnergyDTO.getVapourType()));
                //用电费用统计
                double add = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getPeaksType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getParityType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getLowebbType(), overallEnergyDTO.getPrice()));
                //用水费用统计
                double add1 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getTapType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getSofteningType(), overallEnergyDTO.getPrice()));
                //用气费用统计
                double add2 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCompressType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getNaturalType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getVapourType(), overallEnergyDTO.getPrice()));
                overallEnergyDTO.setEquipmentConPrice(DoubleOperation.add(add, add1, add2));
            }
        }

        return AjaxResult.success(new PageInfo<>(overallEnergyDTOS));
    }

    @Override
    public AjaxResult equipmenteq(Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, String energyType, Integer floor) {
        // 图表查的是所有的，没有分页
        List<EquipmentConsumptionDto> overallEnergyDTOS = overallEnergyDao.selectequipmentEnergyAll(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor);
        // 折线图
        Map<String, EquipmentConsumptionDto> energyMap = new HashMap<>();
        // 饼图
        Map<String, Double> pieGraphMap = new HashMap<>();
        if (overallEnergyDTOS == null || overallEnergyDTOS.isEmpty()) {
            return AjaxResult.success(energyMap);
        }
        Map<Integer, Double> energyTypeMap = new HashMap<>();
        // 初始化Map
        initEnergyTypeMap(energyTypeMap);
        AllCostSum(overallEnergyDTOS, energyMap, energyTypeMap);
        pieGraphPackage(begin, energyType, pieGraphMap, energyTypeMap);

        Map resMap = new HashMap();
        resMap.put("brokenLine", energyMap);
        resMap.put("pieGraph", pieGraphMap);
        return AjaxResult.success(resMap);
    }

    private void pieGraphPackage(Date begin, String energyType, Map<String, Double> pieGraphMap, Map<Integer, Double> energyTypeMap) {
        // 饼图能源
        ArrayList<DateStatistics> dateStatistics = new ArrayList<>(8);
        for (Integer key : energyTypeMap.keySet()) {
            DateStatistics data = new DateStatistics();
            data.setCount(Long.valueOf(key));
            data.setNumber(energyTypeMap.get(key));
            if (key == 6) {
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM");
                data.setDate(format2.format(begin));
            }
            dateStatistics.add(data);
        }

        Map<String, List<DateStatistics>> conjunctByEnergy = totalEnergyService.findConjunctByEnergy(1, 1, dateStatistics);
        if ("powerConsumption".equals(energyType)) {
            pieGraphMap.put("尖电", !CollectionUtils.isEmpty(conjunctByEnergy.get("7")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("7")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
            pieGraphMap.put("峰电", !CollectionUtils.isEmpty(conjunctByEnergy.get("8")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("8")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
            pieGraphMap.put("平电", !CollectionUtils.isEmpty(conjunctByEnergy.get("9")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("9")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
            pieGraphMap.put("谷电", !CollectionUtils.isEmpty(conjunctByEnergy.get("10")) ? doubleTrans(conjunctByEnergy
                                                                                                               .get("10")
                                                                                                               .get(0)
                                                                                                               .getNumber()) : 0.00);
        } else if ("waterConsumption".equals(energyType)) {
            pieGraphMap.put("软化水", !CollectionUtils.isEmpty(conjunctByEnergy.get("2")) ? doubleTrans(conjunctByEnergy
                                                                                                                .get("2")
                                                                                                                .get(0)
                                                                                                                .getNumber()) : 0.00);
            pieGraphMap.put("冷却水", !CollectionUtils.isEmpty(conjunctByEnergy.get("1")) ? doubleTrans(conjunctByEnergy
                                                                                                                .get("1")
                                                                                                                .get(0)
                                                                                                                .getNumber()) : 0.00);
        } else if ("gasConsumption".equals(energyType) || "cleanCompressedAir".equals(energyType) || "lnstrumentAirPressure".equals(energyType)) {
            pieGraphMap.put("压缩空气", !CollectionUtils.isEmpty(conjunctByEnergy.get("4")) ? doubleTrans(conjunctByEnergy
                                                                                                                  .get("4")
                                                                                                                  .get(0)
                                                                                                                  .getNumber()) : 0.00);
            pieGraphMap.put("蒸汽", !CollectionUtils.isEmpty(conjunctByEnergy.get("6")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("6")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
        } else if ("spikesType".equals(energyType)) {
            pieGraphMap.put("尖电", !CollectionUtils.isEmpty(conjunctByEnergy.get("7")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("7")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
        } else if ("peaksType".equals(energyType)) {
            pieGraphMap.put("峰电", !CollectionUtils.isEmpty(conjunctByEnergy.get("8")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("8")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
        } else if ("parityType".equals(energyType)) {
            pieGraphMap.put("平电", !CollectionUtils.isEmpty(conjunctByEnergy.get("9")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("9")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
        } else if ("lowebbType".equals(energyType)) {
            pieGraphMap.put("谷电", !CollectionUtils.isEmpty(conjunctByEnergy.get("10")) ? doubleTrans(conjunctByEnergy
                                                                                                               .get("10")
                                                                                                               .get(0)
                                                                                                               .getNumber()) : 0.00);
        } else if ("coolingType".equals(energyType) || "airHotEnergy".equals(energyType)) {
            pieGraphMap.put("冷却水", !CollectionUtils.isEmpty(conjunctByEnergy.get("1")) ? doubleTrans(conjunctByEnergy
                                                                                                                .get("1")
                                                                                                                .get(0)
                                                                                                                .getNumber()) : 0.00);
        } else if ("softeningType".equals(energyType) || "tapType".equals(energyType)) {
            pieGraphMap.put("软化水", !CollectionUtils.isEmpty(conjunctByEnergy.get("2")) ? doubleTrans(conjunctByEnergy
                                                                                                                .get("2")
                                                                                                                .get(0)
                                                                                                                .getNumber()) : 0.00);
        } else if (null == energyType || "equipmentConPrice".equals(energyType)) {
            pieGraphMap.put("尖电", !CollectionUtils.isEmpty(conjunctByEnergy.get("7")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("7")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
            pieGraphMap.put("峰电", !CollectionUtils.isEmpty(conjunctByEnergy.get("8")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("8")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
            pieGraphMap.put("平电", !CollectionUtils.isEmpty(conjunctByEnergy.get("9")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("9")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
            pieGraphMap.put("谷电", !CollectionUtils.isEmpty(conjunctByEnergy.get("10")) ? doubleTrans(conjunctByEnergy
                                                                                                               .get("10")
                                                                                                               .get(0)
                                                                                                               .getNumber()) : 0.00);
            pieGraphMap.put("软化水", !CollectionUtils.isEmpty(conjunctByEnergy.get("2")) ? doubleTrans(conjunctByEnergy
                                                                                                                .get("2")
                                                                                                                .get(0)
                                                                                                                .getNumber()) : 0.00);
            pieGraphMap.put("冷却水", !CollectionUtils.isEmpty(conjunctByEnergy.get("1")) ? doubleTrans(conjunctByEnergy
                                                                                                                .get("1")
                                                                                                                .get(0)
                                                                                                                .getNumber()) : 0.00);
            pieGraphMap.put("压缩空气", !CollectionUtils.isEmpty(conjunctByEnergy.get("4")) ? doubleTrans(conjunctByEnergy
                                                                                                                  .get("4")
                                                                                                                  .get(0)
                                                                                                                  .getNumber()) : 0.00);
            pieGraphMap.put("蒸汽", !CollectionUtils.isEmpty(conjunctByEnergy.get("6")) ? doubleTrans(conjunctByEnergy
                                                                                                              .get("6")
                                                                                                              .get(0)
                                                                                                              .getNumber()) : 0.00);
        }
    }

    public double doubleTrans(double number) {
        DecimalFormat df = new DecimalFormat("#.00");
        return Double.valueOf(df.format(number));
    }

    @Override
    public AjaxResult workshop(Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, String energyType, Integer floor) throws Exception {
        List<EquipmentConsumptionDto> equipmentConsumptionDtos = overallEnergyDao.energyByWorkShop(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor);
        if (equipmentConsumptionDtos != null && equipmentConsumptionDtos.size() > 0) {
            Map<Integer, Double> priceMap = energyPriceCache.energyPriceMapCaChe();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Double naturalPrice = energyPriceCache.findNaturalByEndDate(sdf.format(end));
            for (EquipmentConsumptionDto overallEnergyDTO : equipmentConsumptionDtos) {
                //用电统计
                overallEnergyDTO.setPowerConsumption(DoubleOperation.add(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPeaksType(), overallEnergyDTO.getParityType(), overallEnergyDTO.getLowebbType()));
                //用水统计
                overallEnergyDTO.setWaterConsumption(DoubleOperation.add(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getTapType(), overallEnergyDTO.getSofteningType()));
                //用气统计
                overallEnergyDTO.setGasConsumption(DoubleOperation.add(overallEnergyDTO.getCompressType(), overallEnergyDTO.getNaturalType(), overallEnergyDTO.getVapourType()));
                //用电费用统计
                double add = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getSpikesType(), priceMap.get(EnergyType.PEAKSTYPE.getId()) * 1.25), DoubleOperation.mul(overallEnergyDTO.getPeaksType(), priceMap.get(EnergyType.PEAKSTYPE.getId())), DoubleOperation.mul(overallEnergyDTO.getParityType(), priceMap.get(EnergyType.PARITYTYPE.getId())), DoubleOperation.mul(overallEnergyDTO.getLowebbType(), priceMap.get(EnergyType.LOWEBBTYPE.getId())));
                //用水费用统计
                double add1 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCoolingType(), priceMap.get(EnergyType.COOLING.getId())), DoubleOperation.mul(overallEnergyDTO.getTapType(), priceMap.get(EnergyType.TAP.getId())), DoubleOperation.mul(overallEnergyDTO.getSofteningType(), priceMap.get(EnergyType.TAP.getId())));
                //用气费用统计
                //  用气费用 =压缩空气的费用+ 蒸汽的费用
                double add2 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCompressType(), priceMap.get(EnergyType.TAP.getId())), DoubleOperation.mul(overallEnergyDTO.getVapourType(), naturalPrice));
                overallEnergyDTO.setEquipmentConPrice(DoubleOperation.add(add, add1, add2));
            }
//            return AjaxResult.success(new PageInfo<>(equipmentConsumptionDtos));
        }
        return AjaxResult.success(new PageInfo<>(equipmentConsumptionDtos));
    }

    @Override
    public AjaxResult workshopChart(Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, String energyType, Integer floor) {
        List<EquipmentConsumptionDto> equipmentConsumptionDtos = overallEnergyDao.energyByWorkShop(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor);
        Map<String, EquipmentConsumptionDto> energyMap = new HashMap<>();
        if (equipmentConsumptionDtos == null || equipmentConsumptionDtos.isEmpty()) {
            return AjaxResult.success(energyMap);
        }
        //定义Map
        Map<Integer, Double> energyTypeMap = new HashMap<>();
        // 饼图
        Map<String, Double> pieGraphMap = new HashMap<>();
        // 初始化Map
        initEnergyTypeMap(energyTypeMap);

        AllCostSum(equipmentConsumptionDtos, energyMap, energyTypeMap);

        pieGraphPackage(begin, energyType, pieGraphMap, energyTypeMap);
        Map resMap = new HashMap();
        resMap.put("brokenLine", energyMap);
        resMap.put("pieGraph", pieGraphMap);
        return AjaxResult.success(resMap);
    }

    private void initEnergyTypeMap(Map<Integer, Double> energyTypeMap) {
        energyTypeMap.put(1, 0.0);
        energyTypeMap.put(2, 0.0);
        energyTypeMap.put(4, 0.0);
        energyTypeMap.put(6, 0.0);
        energyTypeMap.put(7, 0.0);
        energyTypeMap.put(8, 0.0);
        energyTypeMap.put(9, 0.0);
        energyTypeMap.put(10, 0.0);
    }

    /**
     * 所有费用合并
     *
     * @param equipmentConsumptionDtos 不为空
     * @param energyMap
     */
    private void AllCostSum(List<EquipmentConsumptionDto> equipmentConsumptionDtos, Map<String, EquipmentConsumptionDto> energyMap, Map<Integer, Double> energyTypeMap) {
        equipmentConsumptionDtos
                .stream()
                .collect(Collectors.groupingBy(EquipmentConsumptionDto::getCreatedDate))
                .forEach((k, v) -> {
                    v.stream().forEach(groupByEnergy -> {
                        // 单条记录计算
                        singleCostSum(groupByEnergy);
                        // 分组后得到的集合合并
                        if (energyMap.get(k) == null) {
                            energyMap.put(k, groupByEnergy);
                        } else {
                            // 电量
                            energyMap
                                    .get(k)
                                    .setPowerConsumption(DoubleOperation.addNonNullTrack(groupByEnergy.getPowerConsumption(), energyMap
                                            .get(k)
                                            .getPowerConsumption()));
                            // 水量
                            energyMap
                                    .get(k)
                                    .setWaterConsumption(DoubleOperation.addNonNullTrack(groupByEnergy.getWaterConsumption(), energyMap
                                            .get(k)
                                            .getWaterConsumption()));
                            // 气量
                            energyMap
                                    .get(k)
                                    .setGasConsumption(DoubleOperation.addNonNullTrack(groupByEnergy.getGasConsumption(), energyMap
                                            .get(k)
                                            .getGasConsumption()));
                            // 尖电 spikesType
                            energyMap
                                    .get(k)
                                    .setSpikesType(DoubleOperation.addNonNullTrack(groupByEnergy.getSpikesType(), energyMap
                                            .get(k)
                                            .getSpikesType()));
                            // 峰电  peaksType
                            energyMap
                                    .get(k)
                                    .setPeaksType(DoubleOperation.addNonNullTrack(groupByEnergy.getPeaksType(), energyMap
                                            .get(k)
                                            .getPeaksType()));
                            // 平电 parityType
                            energyMap
                                    .get(k)
                                    .setParityType(DoubleOperation.addNonNullTrack(groupByEnergy.getParityType(), energyMap
                                            .get(k)
                                            .getParityType()));
                            // 谷电  lowebbType
                            energyMap
                                    .get(k)
                                    .setLowebbType(DoubleOperation.addNonNullTrack(groupByEnergy.getLowebbType(), energyMap
                                            .get(k)
                                            .getLowebbType()));
                            // 有功电量 activePower
                            energyMap
                                    .get(k)
                                    .setActivePower(DoubleOperation.addNonNullTrack(groupByEnergy.getActivePower(), energyMap
                                            .get(k)
                                            .getActivePower()));
                            // 无功电量 reactivePower
                            energyMap
                                    .get(k)
                                    .setReactivePower(DoubleOperation.addNonNullTrack(groupByEnergy.getReactivePower(), energyMap
                                            .get(k)
                                            .getReactivePower()));
                            // 冷冻水 coolingType
                            energyMap
                                    .get(k)
                                    .setCoolingType(DoubleOperation.addNonNullTrack(groupByEnergy.getCoolingType(), energyMap
                                            .get(k)
                                            .getCoolingType()));
                            // 软化水 softeningType
                            energyMap
                                    .get(k)
                                    .setSofteningType(DoubleOperation.addNonNullTrack(groupByEnergy.getSofteningType(), energyMap
                                            .get(k)
                                            .getSofteningType()));
                            // 自来水  tapType
                            energyMap
                                    .get(k)
                                    .setTapType(DoubleOperation.addNonNullTrack(groupByEnergy.getTapType(), energyMap
                                            .get(k)
                                            .getTapType()));
                            // 仪表压空 lnstrumentAirPressure
                            energyMap
                                    .get(k)
                                    .setLnstrumentAirPressure(DoubleOperation.addNonNullTrack(groupByEnergy.getLnstrumentAirPressure(), energyMap
                                            .get(k)
                                            .getLnstrumentAirPressure()));
                            // 洁净压空 cleanCompressedAir
                            energyMap
                                    .get(k)
                                    .setCleanCompressedAir(DoubleOperation.addNonNullTrack(groupByEnergy.getCleanCompressedAir(), energyMap
                                            .get(k)
                                            .getCleanCompressedAir()));
                            // 空调热能  airHotEnergy
                            energyMap
                                    .get(k)
                                    .setAirHotEnergy(DoubleOperation.addNonNullTrack(groupByEnergy.getAirHotEnergy(), energyMap
                                            .get(k)
                                            .getAirHotEnergy()));
                            // 车间能效 totalDosage
                            energyMap
                                    .get(k)
                                    .setTotalDosage(DoubleOperation.addNonNullTrack(groupByEnergy.getTotalDosage(), energyMap
                                            .get(k)
                                            .getTotalDosage()));
                            //单位耗能  unitEnergy
                            energyMap
                                    .get(k)
                                    .setUnitEnergy(DoubleOperation.addNonNullTrack(groupByEnergy.getUnitEnergy(), energyMap
                                            .get(k)
                                            .getUnitEnergy()));
                            //总费用
                            energyMap
                                    .get(k)
                                    .setEquipmentConPrice(DoubleOperation.addNonNullTrack(groupByEnergy.getEquipmentConPrice(), energyMap
                                            .get(k)
                                            .getEquipmentConPrice()));
                        }
                        /**
                         * 对各种能源类型进行相处理
                         */
                        // 冷却水+空调热能
                        energyTypeMap.put(1, DoubleOperation.addNonNullTrack(groupByEnergy.getCoolingType(), groupByEnergy.getAirHotEnergy(), energyTypeMap.get(1)));
                        energyTypeMap.put(2, DoubleOperation.addNonNullTrack(groupByEnergy.getTapType(), groupByEnergy.getSofteningType(), energyTypeMap.get(2)));
                        energyTypeMap.put(4, DoubleOperation.addNonNullTrack(groupByEnergy.getCompressType(), energyTypeMap.get(4)));
                        energyTypeMap.put(6, DoubleOperation.addNonNullTrack(groupByEnergy.getVapourType(), energyTypeMap.get(6)));
                        energyTypeMap.put(7, DoubleOperation.addNonNullTrack(groupByEnergy.getSpikesType(), energyTypeMap.get(7)));
                        energyTypeMap.put(8, DoubleOperation.addNonNullTrack(groupByEnergy.getPeaksType(), energyTypeMap.get(8)));
                        energyTypeMap.put(9, DoubleOperation.addNonNullTrack(groupByEnergy.getParityType(), energyTypeMap.get(9)));
                        energyTypeMap.put(10, DoubleOperation.addNonNullTrack(groupByEnergy.getLowebbType(), energyTypeMap.get(10)));

                    });
                });
    }

    /**
     * 单条记录 用量和费用合计
     *
     * @param groupByEnergy
     */
    private void singleCostSum(EquipmentConsumptionDto groupByEnergy) {
        //用电统计
        groupByEnergy.setPowerConsumption(DoubleOperation.add(groupByEnergy.getSpikesType(), groupByEnergy.getPeaksType(), groupByEnergy.getParityType(), groupByEnergy.getLowebbType()));
        //用水统计
        groupByEnergy.setWaterConsumption(DoubleOperation.add(groupByEnergy.getCoolingType(), groupByEnergy.getTapType(), groupByEnergy.getSofteningType()));
        //用气统计
        groupByEnergy.setGasConsumption(DoubleOperation.add(groupByEnergy.getCompressType(), groupByEnergy.getNaturalType(), groupByEnergy.getVapourType()));
        //用电费用统计
        double add = DoubleOperation.add(DoubleOperation.mul(groupByEnergy.getSpikesType(), groupByEnergy.getPrice()), DoubleOperation.mul(groupByEnergy.getPeaksType(), groupByEnergy.getPrice()), DoubleOperation.mul(groupByEnergy.getParityType(), groupByEnergy.getPrice()), DoubleOperation.mul(groupByEnergy.getLowebbType(), groupByEnergy.getPrice()));
        //用水费用统计
        double add1 = DoubleOperation.add(DoubleOperation.mul(groupByEnergy.getCoolingType(), groupByEnergy.getPrice()), DoubleOperation.mul(groupByEnergy.getTapType(), groupByEnergy.getPrice()), DoubleOperation.mul(groupByEnergy.getSofteningType(), groupByEnergy.getPrice()));
        //用气费用统计
        double add2 = DoubleOperation.add(DoubleOperation.mul(groupByEnergy.getCompressType(), groupByEnergy.getPrice()), DoubleOperation.mul(groupByEnergy.getNaturalType(), groupByEnergy.getPrice()), DoubleOperation.mul(groupByEnergy.getVapourType(), groupByEnergy.getPrice()));
        groupByEnergy.setEquipmentConPrice(DoubleOperation.add(add, add1, add2));
    }

    @Override
    public AjaxResult building(Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, String energyType, Integer floor) throws Exception {
        List<EquipmentConsumptionDto> equipmentConsumptionDtos = overallEnergyDao.buildingByWorkShop(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor);
        if (equipmentConsumptionDtos != null && equipmentConsumptionDtos.size() > 0) {
            Map<Integer, Double> priceMap = energyPriceCache.energyPriceMapCaChe();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Double naturalPrice = energyPriceCache.findNaturalByEndDate(sdf.format(end));
            for (EquipmentConsumptionDto overallEnergyDTO : equipmentConsumptionDtos) {
                //用电统计
                overallEnergyDTO.setPowerConsumption(DoubleOperation.add(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPeaksType(), overallEnergyDTO.getParityType(), overallEnergyDTO.getLowebbType()));
                //用水统计
                overallEnergyDTO.setWaterConsumption(DoubleOperation.add(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getTapType(), overallEnergyDTO.getSofteningType()));
                //用气统计
                overallEnergyDTO.setGasConsumption(DoubleOperation.add(overallEnergyDTO.getCompressType(), overallEnergyDTO.getNaturalType(), overallEnergyDTO.getVapourType()));
                //用电费用统计
                double add = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getSpikesType(), priceMap.get(EnergyType.PEAKSTYPE.getId()) * 1.25), DoubleOperation.mul(overallEnergyDTO.getPeaksType(), priceMap.get(EnergyType.PEAKSTYPE.getId())), DoubleOperation.mul(overallEnergyDTO.getParityType(), priceMap.get(EnergyType.PARITYTYPE.getId())), DoubleOperation.mul(overallEnergyDTO.getLowebbType(), priceMap.get(EnergyType.LOWEBBTYPE.getId())));
                //用水费用统计
                double add1 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCoolingType(), priceMap.get(EnergyType.COOLING.getId())), DoubleOperation.mul(overallEnergyDTO.getTapType(), priceMap.get(EnergyType.TAP.getId())), DoubleOperation.mul(overallEnergyDTO.getSofteningType(), priceMap.get(EnergyType.TAP.getId())));
                //用气费用统计
                //用气费用 =压缩空气的费用+ 蒸汽的费用
                double add2 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCompressType(), priceMap.get(EnergyType.TAP.getId())), DoubleOperation.mul(overallEnergyDTO.getVapourType(), naturalPrice));
                overallEnergyDTO.setEquipmentConPrice(DoubleOperation.add(add, add1, add2));

            }
//            return AjaxResult.success(new PageInfo<>(equipmentConsumptionDtos));
        }
//        return AjaxResult.error("数据不对");
        return AjaxResult.success(new PageInfo<>(equipmentConsumptionDtos));

    }

    @Override
    public AjaxResult buildingChart(Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, String energyType, Integer floor) {
        List<EquipmentConsumptionDto> equipmentConsumptionDtos = overallEnergyDao.buildingByWorkShop(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor);
        Map<String, EquipmentConsumptionDto> energyMap = new HashMap<>();
        // 饼图
        Map<String, Double> pieGraphMap = new HashMap<>();
        if (equipmentConsumptionDtos == null || equipmentConsumptionDtos.isEmpty()) {
            return AjaxResult.success(energyMap);
        }
        // energyTypeMap  用来存储
        Map<Integer, Double> energyTypeMap = new HashMap<>();
        AllCostSum(equipmentConsumptionDtos, energyMap, energyTypeMap);
        // 封装饼图 Map
        pieGraphPackage(begin, energyType, pieGraphMap, energyTypeMap);

        Map resMap = new HashMap();
        resMap.put("brokenLine", energyMap);
        resMap.put("pieGraph", pieGraphMap);
        return AjaxResult.success(resMap);
    }

    @Override
    public List<EquipmentConsumptionDto> equipmentByReport(Integer currPage, Integer pageSize, Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, Integer floor) {
        List<EquipmentConsumptionDto> overallEnergyDTOS = overallEnergyDao.selectequipmentEnergy(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor, null);
        if (overallEnergyDTOS != null && overallEnergyDTOS.size() > 0) {
            for (EquipmentConsumptionDto overallEnergyDTO : overallEnergyDTOS) {
                //用电统计
                overallEnergyDTO.setPowerConsumption(DoubleOperation.add(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPeaksType(), overallEnergyDTO.getParityType(), overallEnergyDTO.getLowebbType()));
                //用水统计
                overallEnergyDTO.setWaterConsumption(DoubleOperation.add(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getTapType(), overallEnergyDTO.getSofteningType()));
                //用气统计
                overallEnergyDTO.setGasConsumption(DoubleOperation.add(overallEnergyDTO.getCompressType(), overallEnergyDTO.getNaturalType(), overallEnergyDTO.getVapourType()));
                //用电费用统计
                double add = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getPeaksType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getParityType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getLowebbType(), overallEnergyDTO.getPrice()));
                //用水费用统计
                double add1 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getTapType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getSofteningType(), overallEnergyDTO.getPrice()));
                //用气费用统计
                double add2 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCompressType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getNaturalType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getVapourType(), overallEnergyDTO.getPrice()));
                overallEnergyDTO.setEquipmentConPrice(DoubleOperation.add(add, add1, add2));
            }
        }

        return overallEnergyDTOS;
    }

    @Override
    public List<EquipmentConsumptionDto> workshopByReport(Integer currPage, Integer pageSize, Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, Integer floor) {
        List<EquipmentConsumptionDto> equipmentConsumptionDtos = overallEnergyDao.energyByWorkShop(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor);
        if (equipmentConsumptionDtos != null && equipmentConsumptionDtos.size() > 0) {
            for (EquipmentConsumptionDto overallEnergyDTO : equipmentConsumptionDtos) {
                //用电统计
                overallEnergyDTO.setPowerConsumption(DoubleOperation.add(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPeaksType(), overallEnergyDTO.getParityType(), overallEnergyDTO.getLowebbType()));
                //用水统计
                overallEnergyDTO.setWaterConsumption(DoubleOperation.add(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getTapType(), overallEnergyDTO.getSofteningType()));
                //用气统计
                overallEnergyDTO.setGasConsumption(DoubleOperation.add(overallEnergyDTO.getCompressType(), overallEnergyDTO.getNaturalType(), overallEnergyDTO.getVapourType()));
                //用电费用统计
                double add = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getPeaksType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getParityType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getLowebbType(), overallEnergyDTO.getPrice()));
                //用水费用统计
                double add1 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getTapType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getSofteningType(), overallEnergyDTO.getPrice()));
                //用气费用统计
                double add2 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCompressType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getNaturalType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getVapourType(), overallEnergyDTO.getPrice()));
                overallEnergyDTO.setEquipmentConPrice(DoubleOperation.add(add, add1, add2));
            }
//            return AjaxResult.success(new PageInfo<>(equipmentConsumptionDtos));
        }
        return equipmentConsumptionDtos;
    }

    @Override
    public List<EquipmentConsumptionDto> buildingByReport(Integer currPage, Integer pageSize, Long buildingId, List<Integer> workshopIds, List<Integer> deviceIds, Integer classifyId, String deviceName, Date begin, Date end, Integer floor) {
        List<EquipmentConsumptionDto> equipmentConsumptionDtos = overallEnergyDao.buildingByWorkShop(buildingId, workshopIds, deviceIds, classifyId, deviceName, DateUtils.DateToStringYmd(begin), DateUtils.DateToStringYmd(end), floor);
        if (equipmentConsumptionDtos != null && equipmentConsumptionDtos.size() > 0) {
            for (EquipmentConsumptionDto overallEnergyDTO : equipmentConsumptionDtos) {
                //用电统计
                overallEnergyDTO.setPowerConsumption(DoubleOperation.add(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPeaksType(), overallEnergyDTO.getParityType(), overallEnergyDTO.getLowebbType()));
                //用水统计
                overallEnergyDTO.setWaterConsumption(DoubleOperation.add(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getTapType(), overallEnergyDTO.getSofteningType()));
                //用气统计
                overallEnergyDTO.setGasConsumption(DoubleOperation.add(overallEnergyDTO.getCompressType(), overallEnergyDTO.getNaturalType(), overallEnergyDTO.getVapourType()));
                //用电费用统计
                double add = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getSpikesType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getPeaksType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getParityType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getLowebbType(), overallEnergyDTO.getPrice()));
                //用水费用统计
                double add1 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCoolingType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getTapType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getSofteningType(), overallEnergyDTO.getPrice()));
                //用气费用统计
                double add2 = DoubleOperation.add(DoubleOperation.mul(overallEnergyDTO.getCompressType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getNaturalType(), overallEnergyDTO.getPrice()), DoubleOperation.mul(overallEnergyDTO.getVapourType(), overallEnergyDTO.getPrice()));
                overallEnergyDTO.setEquipmentConPrice(DoubleOperation.add(add, add1, add2));

            }
//            return AjaxResult.success(new PageInfo<>(equipmentConsumptionDtos));
        }
//        return AjaxResult.error("数据不对");
        return equipmentConsumptionDtos;
    }


}


